import { http } from "@/utils/http";
import { idpUrlApi } from "../utils";

export type searchBranchDto = {
  name: string;
  pageNum: number;
  pageSize: number;
};

export type Result = {
  list: Array<any>;
  total?: number;
};

// 获取分页信息
export const getPage = (params: searchBranchDto) => {
  return http.request<Result>("get", idpUrlApi("branch/page"), { params });
};

// 获取分支机构列表
export const getList = () => {
  return http.request<any[]>("get", idpUrlApi("branch/list"));
};

export type addBranchDto = {
  name: string;
  cityCode: string;
  lng: string;
  lat: string;
  planePic: string;
  titlePic: string;
  admin: string;
  tel: string;
};
// 添加分支机构
export const addBranch = (data: addBranchDto) => {
  return http.request("post", idpUrlApi("branch/add"), { data });
};

export type updateBranchDto = {
  id: string;
  name: string;
  cityCode: string;
  lng: string;
  lat: string;
  planePic: string;
  titlePic: string;
  admin: string;
  tel: string;
};

// 添加分支机构
export const updateBranch = (data: updateBranchDto) => {
  return http.request("put", idpUrlApi("branch/update"), { data });
};

// 删除分支机构
export const deleteBranch = (id: string) => {
  return http.request("delete", idpUrlApi("branch/" + id));
};

export type branchBindUserDto = {
  userIds: string[];
  branchId: string;
};

// 绑定分支机构和用户的管理关系
export const branchBindUser = (data: branchBindUserDto) => {
  return http.request("put", idpUrlApi("branch/bindUser"), { data });
};

// 获取部门已经绑定的用户
export const getBoundUserIds = (branchId: string) => {
  return http.request<string[]>(
    "get",
    idpUrlApi("branch/boundUser/" + branchId)
  );
};
// 获取地图json数据
export const getMapJson = (code: string) => {
  return http.request<string>("get", idpUrlApi("static/map/" + code + ".json"));
};

export type idpOfficePointParams = {
  branchId: string;
  backGroundImg: string;
  jsonStr: string;
};

// saveOfficePoint 保存办公室标记点信息
export const saveOfficePoint = (data: idpOfficePointParams) => {
  return http.request("post", idpUrlApi("map/officePoint"), { data });
};

// getOfficePoint 获取办公室标记点信息
export const getOfficePoint = (branchId: string) => {
  return http.request<any>("get", idpUrlApi("map/officePoint/" + branchId));
};

// 上传文件
export const UploadPic = (option: any) => {
  const formData = new FormData();
  // @ts-ignore
  formData.append("file", option.file);
  return http
    .post(
      idpUrlApi("branch/pic"),
      { data: formData },
      {
        headers: {
          "Content-Type": "multipart/form-data"
        }
      }
    )
    .then(res => {
      return res;
    });
};

// 获取请求url上的参数
export const getRequestParams = (key: string) => {
  const temp = window.location.href.split("?")[1];
  const pram = new URLSearchParams("?" + temp);
  return pram.get(key);
};
